import jfRequest, { upRequest } from "../../index";
import JFRequest from "@/service/request";
import { ILoginReq, ILoginResult, UploadResult } from "./types";
import { IResultType } from "types/request";
import LocalCache from "@/utils/cache";
import { APPID, APP_KEY, GET_ORGCODE_URL } from "@/service/request/config";
export function accountLoginRequest(data: ILoginReq) {
  return jfRequest.post<IResultType<ILoginResult>>({
    url: "/api/v1/login",
    data,
  });
}
export function requestSysInfo(data: any) {
  return jfRequest.get<IResultType>({
    url: "/api/v1/getInfo",
    data,
    showLoading: false,
  });
}

// 获取token和orgCode
export async function getTokenAddOrgCode(
  appToken: String,
  timeStamp: String,
  accessToken: String
) {
  const appId = "JF-ISWCC-WEB";
  const jfRequest = new JFRequest({
    baseURL: GET_ORGCODE_URL,
    timeout: 30000,
    interceptors: {
      requestIntercepter: (config) => {
        config.headers.timeStamp = timeStamp;
        config.headers.appId = appId;
        config.headers.appToken = appToken;
        return config;
      },
      requestInterceptorCatch: (err) => {
        return err;
      },
      responseIntercepter: (config) => {
        return config;
      },
      responseInterceptorCatch: (err) => {
        return err;
      },
    },
  });
  const data = {
    appId: APPID,
    appKey: APP_KEY,
    accessToken,
  };
  return jfRequest.post<IResultType<any>>({
    url: "/service/isptl/open/public/loginByAccessToken",
    showLoading: true,
    data: {
      data,
    },
  });
}

// 上传文件
export function uploadFile(headers: any, data: any) {
  return upRequest.post<UploadResult>({
    url: "/restful/files/uapi/upload/push",
    showLoading: true,
    headers,
    data,
  });
}

/**
 * @description: 检查文件是否已上传
 * @param {any} data
 * @return {*}
 */
export function uploadCheckReq(data: any) {
  let loginInfo: any = getSSLoginInfo();
  return upRequest.get<any>({
    url: "/restful/files/uapi/upload/check",
    headers: {
      appToken: loginInfo?.appToken,
      accessToken: loginInfo?.accessToken,
      operateCode: "test",
    },
    params: data,
    showLoading: false
  });
}

/**
 * @description: 文件切片上传
 * @param {any} data
 * @return {*}
 */
export function fileUploadAddReq(data: any) {
  let loginInfo: any = getSSLoginInfo();
  let formData = new FormData();
  formData.append("sliceInfo.sliceIndex", data.sliceInfo.sliceIndex);
  formData.append("sliceInfo.sliceSize", data.sliceInfo.sliceSize);
  formData.append("sliceInfo.totalSize", data.sliceInfo.totalSize);
  formData.append("sliceInfo.sliceTotal", data.sliceInfo.sliceTotal);
  formData.append("file", data.file); // 当前分片的文件流
  formData.append("md5", data.md5); // 整个文件的MD5唯一标识码，不是分片
  formData.append("name", data.name); // 文件的名称
  formData.append("catalog", data.catalog);
  formData.append("type", data.type);
  return upRequest.post({
    url: "/restful/files/uapi/upload/add",
    headers: {
      "Content-Type": "multipart/form-data;",
      appToken: loginInfo?.appToken,
      accessToken: loginInfo?.accessToken,
      operateCode: "test",
    },
    data: formData,
    showLoading: false
  });
}
/**
 * @description: 文件合并上传-- 分片之后
 * @param {any} data
 * @return {*}
 */
export function fileUploadReq(data: any) {
  let loginInfo: any = getSSLoginInfo();
  return upRequest.post({
    url: "/restful/files/uapi/upload/merge",
    headers: {
      appToken: loginInfo?.appToken,
      accessToken: loginInfo?.accessToken,
      operateCode: "test",
    },
    data: data,
    showLoading: false
  });
}

/**
 * @description: 文件整个上传
 * @param {any} data
 * @return {*}
 */
export function fileUploadAll(data: any) {
  let loginInfo: any = getSSLoginInfo();
  let formData = new FormData();
  formData.append("catalog", data.catalog);
  formData.append("file", data.file);
  formData.append("myFileName", data.myFileName);
  formData.append("archived", data.archived);
  return upRequest.post({
    url: "/restful/files/uapi/upload/push",
    headers: {
      "Content-Type": "multipart/form-data;",
      appToken: loginInfo.appToken,
      accessToken: loginInfo.accessToken,
      operateCode: "test",
    },
    data: formData,
    showLoading: false
  });
}
/**
 * @description: 获取用户信息
 * @return {*}
 */
export function getSSLoginInfo() {
  let obj = LocalCache.getSession("userInfo");
  return obj;
}
